package com.ease.util {

   import flash.external.ExternalInterface;


   /**
    * This class provides the utilities needed to access browser information.
    */
   public class BrowserUtil {

      /**
       * Microsoft Internet Explorer identification string.
       * Use this constant to check against the 'name' property
       * to check if the current browser is MSIE.
       */
      public static const BROWSER_MSIE:String = "msie";

      public static const DISPLAY_NAME_MSIE:String = "MsIE";

      /**
       * Mozilla Firefox identification string.
       * Use this constant to check against the 'name' property
       * to check if the current browser is Firefox.
       */
      public static const BROWSER_FIREFOX:String = "firefox";

      public static const DISPLAY_NAME_FIREFOX:String = "Firefox";

      /**
       * Google Chrome identification string.
       * Use this constant to check against the 'name' property
       * to check if the current browser is Chrome.
       */
      public static const BROWSER_CHROME:String = "chrome";

      public static const DISPLAY_NAME_CHROME:String = "Chrome";

      /**
       * @private
       * A list of browser info objects. In case a new browser is
       * added to the supported list adding a info object and
       * a name constant to this class would enable it to work
       * with the new browser.
       */
      private static const _BROWSER_INFO:Array /* IBrowserInfo */ =
         [new FirefoxBrowserInfo(), new MsieBrowserInfo(), new ChromeBrowserInfo()];

      /**
       * @private
       */
      private static var _browserSupported:Boolean = false;

      /**
       * @private
       */
      private static var _name:String = null;

      /**
       * @private
       */
      private static var _version:String = null;

      /**
       * @private
       */
      private static var _os:String = null;

      /**
       * @private
       */
      private static var _arch:String = null;

      /**
       * @private
       */
      private static var _cookieEnabled:Boolean = false;

      init();

      /**
       * @private
       * Initialize the static fields with information about the
       * current browser.
       */
      private static function init():void {
         if (!ExternalInterface.available) {
            return;
         }
         var ua:String =
            ExternalInterface.call("function() {return window.navigator.userAgent;}");
         for(var i:int = 0; i < _BROWSER_INFO.length; i++) {
            var info:IBrowserInfo = IBrowserInfo(_BROWSER_INFO[i]);
            if (info.detect(ua)) {
               _browserSupported = true;
               _name = info.name;
               _version = info.version;
               _os = info.os;
               _arch = info.arch;
               _cookieEnabled = info.cookieEnabled;
               break;
            }
         }

      }

      /**
       * Checks if the current browser is supported.
       *
       * @return
       *    True if the browser is supported,
       *    false if it is not or if the code is not running
       *    in a browser environment or if javascript is not enabled
       *    in the current browser.
       */
      public static function get browserSupported():Boolean {
         return _browserSupported;
      }

      /**
       * Get the version string for this browser.
       * Returns null if the browser is not supported.
       */
      public static function get version():String {
         return _version;
      }

      /**
       * Get the name of this browser.
       * Returns null if the browser is not supported.
       */
      public static function get name():String {
         return _name;
      }

      /*
      * Get the display name for the browser.
      */
      public static function get displayName():String{
         if(name == BROWSER_MSIE){
            return DISPLAY_NAME_MSIE;
         }
         if(name == BROWSER_FIREFOX){
            return DISPLAY_NAME_FIREFOX;
         }
         if(name == BROWSER_CHROME){
            return DISPLAY_NAME_CHROME;
         }
         return name;
      }

      /**
       * Get the underlying OS as identified by this browser.
       * Returns null if the browser is not supported.
       */
      public static function get os():String {
         return _os;
      }

      /**
       * Get the CPU architecture information(32bit or 64bit).
       * Returns null if the browser is not supported.
       */
      public static function get arch():String {
         return _arch;
      }

      /**
       * Check if cookies are enabled.
       * Returns false if the browser is not supported.
       */
      public static function get cookieEnabled():Boolean {
         return _cookieEnabled;
      }

      /**
       * Used to reload the application.
       */
      public static function reloadApp():void {
         // Use javascript to reload the page.
         ExternalInterface.call("function() { location.reload(true); }");
      }
   }
}